{% extends 'base.html' %}

{% block title %}环境数据清洗{% endblock %}

{% block content %}
<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        <h4 class="mb-0">异常数据检测</h4>
    </div>
    <div class="card-body">
        <!-- 搜索表单 -->
        <form method="get" class="mb-4">
            <div class="row g-3">
                <div class="col-md-3">
                    <input type="text" class="form-control" name="search_region" value="{{ search_region }}" placeholder="搜索区域...">
                </div>
                <div class="col-md-2">
                    <input type="date" class="form-control" name="date_from" value="{{ date_from }}" placeholder="开始日期">
                </div>
                <div class="col-md-2">
                    <input type="date" class="form-control" name="date_to" value="{{ date_to }}" placeholder="结束日期">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">搜索</button>
                </div>
                <div class="col-md-2">
                    <a href="{{ url_for('cleaning.environment_clean') }}" class="btn btn-secondary w-100">重置</a>
                </div>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>区域</th>
                        <th>日期</th>
                        <th>温度(℃)</th>
                        <th>降雨量(mm)</th>
                        <th>光照强度(lux)</th>
                        <th>土壤类型</th>
                        <th>土壤肥力</th>
                        <th>异常原因</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in anomalies %}
                    <tr>
                        <td>{{ item.region }}</td>
                        <td>{{ item.date.strftime('%Y-%m-%d') }}</td>
                        <td>{{ item.temperature }}</td>
                        <td>{{ item.rainfall }}</td>
                        <td>{{ item.light_intensity }}</td>
                        <td>{{ item.soil_type }}</td>
                        <td>{{ item.soil_fertility }}</td>
                        <td>
                            {% if item.temperature < -50 or item.temperature > 50 %}温度异常{% endif %}
                            {% if item.rainfall < 0 %}降雨量异常{% endif %}
                            {% if item.light_intensity < 0 %}光照强度异常{% endif %}
                            {% if item.date > now %}日期异常{% endif %}
                            {% if not item.region %}区域为空{% endif %}
                        </td>
                        <td>
                            <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#fixModal{{ item.id }}">
                                修正
                            </button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if anomalies_pagination and anomalies_pagination.pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item {% if not anomalies_pagination.has_prev %}disabled{% endif %}">
                    <a class="page-link" href="{% if anomalies_pagination.has_prev %}{{ url_for('cleaning.environment_clean', page=anomalies_pagination.prev_num, search_region=search_region, date_from=date_from, date_to=date_to) }}{% else %}#{% endif %}">
                        上一页
                    </a>
                </li>
                
                {% for page in anomalies_pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                    {% if page %}
                        <li class="page-item {% if page == anomalies_pagination.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('cleaning.environment_clean', page=page, search_region=search_region, date_from=date_from, date_to=date_to) }}">
                                {{ page }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">...</span>
                        </li>
                    {% endif %}
                {% endfor %}
                
                <li class="page-item {% if not anomalies_pagination.has_next %}disabled{% endif %}">
                    <a class="page-link" href="{% if anomalies_pagination.has_next %}{{ url_for('cleaning.environment_clean', page=anomalies_pagination.next_num, search_region=search_region, date_from=date_from, date_to=date_to) }}{% else %}#{% endif %}">
                        下一页
                    </a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<div class="card">
    <div class="card-header bg-primary text-white">
        <h4 class="mb-0">重复数据检测</h4>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>区域</th>
                        <th>日期</th>
                        <th>重复次数</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in duplicates %}
                    <tr>
                        <td>{{ item.region }}</td>
                        <td>{{ item.date.strftime('%Y-%m-%d') }}</td>
                        <td>{{ item.count }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

{% for item in anomalies %}
<div class="modal fade" id="fixModal{{ item.id }}" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">修正异常数据</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ url_for('cleaning.fix_environment_anomaly', id=item.id) }}" method="post">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">区域</label>
                        <input type="text" class="form-control" name="region" value="{{ item.region }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">日期</label>
                        <input type="date" class="form-control" name="date" value="{{ item.date.strftime('%Y-%m-%d') }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">温度(℃)</label>
                        <input type="number" step="0.1" class="form-control" name="temperature" value="{{ item.temperature }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">降雨量(mm)</label>
                        <input type="number" step="0.1" class="form-control" name="rainfall" value="{{ item.rainfall }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">光照强度(lux)</label>
                        <input type="number" step="0.1" class="form-control" name="light_intensity" value="{{ item.light_intensity }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">土壤类型</label>
                        <select class="form-select" name="soil_type">
                            <option value="">请选择土壤类型</option>
                            <option value="砂质土" {% if item.soil_type == '砂质土' %}selected{% endif %}>砂质土</option>
                            <option value="壤土" {% if item.soil_type == '壤土' %}selected{% endif %}>壤土</option>
                            <option value="粘土" {% if item.soil_type == '粘土' %}selected{% endif %}>粘土</option>
                            <option value="砂壤土" {% if item.soil_type == '砂壤土' %}selected{% endif %}>砂壤土</option>
                            <option value="粘壤土" {% if item.soil_type == '粘壤土' %}selected{% endif %}>粘壤土</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">土壤肥力</label>
                        <select class="form-select" name="soil_fertility">
                            <option value="">请选择土壤肥力</option>
                            <option value="高" {% if item.soil_fertility == '高' %}selected{% endif %}>高</option>
                            <option value="中" {% if item.soil_fertility == '中' %}selected{% endif %}>中</option>
                            <option value="低" {% if item.soil_fertility == '低' %}selected{% endif %}>低</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endfor %}
{% endblock %} 